;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;                                                                ;;;
;;;                           cl-famix                             ;;;
;;;                     (Common Lisp FAMIX library)                ;;;
;;;                        by Adrian DOZSA                         ;;;
;;;                                                                ;;;
;;;                         Main Functions                         ;;;
;;;                                                                ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;; cl-famix package
 
(in-package :cl-famix)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;; Main functions

(defun extract-model (path &key name verbose)
  (when verbose (format t "~%Starting model extraction..."))
  (when verbose (format t "~%Initializing."))
  (init-work)
  (when verbose (format t "~%Prescanning files for preliminary work."))
  (prescan-files path)
  (when verbose (format t "~%Scanning files.")) 
  (process-files path :verbose verbose)
  (when name
    (setf (name *model*) name))
  (when verbose (format t "~%~%Creating references table."))
  (create-xref-table path)
  (when verbose (format t "~%Creating links between objects. (this may take a while...)"))
  (create-references *model*)
  (when verbose (format t "~%Creating calls between objects. (this may take a while...)"))
  (create-calls *model*)
  (when verbose (format t "~%Creating macro-expansions. (this may take a while...)"))
  (create-macro-expand-table path)
  (create-expands *model*)
  (when verbose (format t "~%Calculating complexity."))
  (calculate-complexity *model*)
  (cleanup-work)
  (when verbose (format t "~%Done."))
  *model*)

(defun model-to-mse (model)
  (let ((stream (make-string-output-stream)))
    (print-to-mse model stream)
    (get-output-stream-string stream)))

(defun save-model-to-mse-file (model filename)
  (with-open-file (file filename 
                        :direction :output 
                        :if-exists :overwrite
                        :if-does-not-exist :create)
    (format file (model-to-mse model))))

(defun save-model-to-mse-file-unbuffered (model filename)
  (with-open-file (file filename
                        :direction :output 
                        :if-exists :overwrite
                        :if-does-not-exist :create)
    (print-to-mse model file)))

(defun extract-model-to-file (from-path to-filename &key name verbose)
  (save-model-to-mse-file-unbuffered
   (extract-model from-path :name name :verbose verbose)
   to-filename))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
